package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TWellCurInforBo;
import com.kld.yqyj.domain.vo.TWellCurInforVo;
import com.kld.yqyj.domain.TWellCurInfor;
import com.kld.yqyj.mapper.TWellCurInforMapper;
import com.kld.yqyj.service.ITWellCurInforService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 井当前基础信息Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TWellCurInforServiceImpl implements ITWellCurInforService {

    private final TWellCurInforMapper baseMapper;

    /**
     * 查询井当前基础信息
     *
     * @param wellId 井当前基础信息主键
     * @return 井当前基础信息
     */
    @Override
    public TWellCurInforVo queryById(String wellId){
        return baseMapper.selectVoById(wellId);
    }

    /**
     * 查询井当前基础信息列表
     *
     * @param bo 井当前基础信息
     * @return 井当前基础信息
     */
    @Override
    public TableDataInfo<TWellCurInforVo> queryPageList(TWellCurInforBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TWellCurInfor> lqw = buildQueryWrapper(bo);
        Page<TWellCurInforVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询井当前基础信息列表
     *
     * @param bo 井当前基础信息
     * @return 井当前基础信息
     */
    @Override
    public List<TWellCurInforVo> queryList(TWellCurInforBo bo) {
        LambdaQueryWrapper<TWellCurInfor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TWellCurInfor> buildQueryWrapper(TWellCurInforBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TWellCurInfor> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWellName()), TWellCurInfor::getWellName, bo.getWellName());
        lqw.eq(StringUtils.isNotBlank(bo.getGeoId()), TWellCurInfor::getGeoId, bo.getGeoId());
        lqw.like(StringUtils.isNotBlank(bo.getGeoName()), TWellCurInfor::getGeoName, bo.getGeoName());
        lqw.eq(StringUtils.isNotBlank(bo.getZyqId()), TWellCurInfor::getZyqId, bo.getZyqId());
        lqw.like(StringUtils.isNotBlank(bo.getZyqName()), TWellCurInfor::getZyqName, bo.getZyqName());
        lqw.eq(StringUtils.isNotBlank(bo.getZdId()), TWellCurInfor::getZdId, bo.getZdId());
        lqw.like(StringUtils.isNotBlank(bo.getZdName()), TWellCurInfor::getZdName, bo.getZdName());
        lqw.eq(bo.getTCRQ() != null, TWellCurInfor::getTCRQ, bo.getTCRQ());
        lqw.eq(StringUtils.isNotBlank(bo.getJB()), TWellCurInfor::getJB, bo.getJB());
        lqw.eq(StringUtils.isNotBlank(bo.getYl1()), TWellCurInfor::getYl1, bo.getYl1());
        lqw.eq(StringUtils.isNotBlank(bo.getYl2()), TWellCurInfor::getYl2, bo.getYl2());
        return lqw;
    }

    /**
     * 新增井当前基础信息
     *
     * @param bo 井当前基础信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TWellCurInforBo bo) {
        TWellCurInfor add = BeanUtil.toBean(bo, TWellCurInfor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWellId(add.getWellId());
        }
        return flag;
    }

    /**
     * 修改井当前基础信息
     *
     * @param bo 井当前基础信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TWellCurInforBo bo) {
        TWellCurInfor update = BeanUtil.toBean(bo, TWellCurInfor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TWellCurInfor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除井当前基础信息
     *
     * @param wellIds 需要删除的井当前基础信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
